package com.haitaoit.chengquan.module.home.activity.fabu;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Message;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.base.ExitApplication;
import com.haitaoit.chengquan.base.QBaseActivity;
import com.haitaoit.chengquan.bean.ImgObj;
import com.haitaoit.chengquan.business.data.PublicServiceHttp;
import com.haitaoit.chengquan.business.http.bean.ParamObj;
import com.haitaoit.chengquan.business.request.PublicServiceReq;
import com.haitaoit.chengquan.business.request.ReqImgObj;
import com.haitaoit.chengquan.module.home.adapter.forum.QImageAdapter;
import com.haitaoit.chengquan.utils.Base64Util;
import com.haitaoit.chengquan.utils.FileFinal;
import com.haitaoit.chengquan.utils.PreferenceUtils;
import com.haitaoit.chengquan.utils.QLBitmapUtil;
import com.haitaoit.chengquan.utils.TakeImage;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.haitaoit.chengquan.view.CircleImageView;
import com.haitaoit.chengquan.view.MyGridView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.ArrayList;
import java.util.List;


public class QSendServiceActivity extends QBaseActivity {

    @ViewInject(R.id.back)
    ImageView       back;
    @ViewInject(R.id.title)
    TextView        title;
    //表头部信息
    @ViewInject(R.id.title_img)
    CircleImageView title_img;
    @ViewInject(R.id.service_type_tv)
    TextView        service_type_tv;
    @ViewInject(R.id.service_title)
    EditText        service_title;
    @ViewInject(R.id.service_desc)
    EditText        service_desc;
    @ViewInject(R.id.service_price_start)
    EditText        service_price_start;
    @ViewInject(R.id.service_price_end)
    EditText        service_price_end;

    /**
     * 表中间信息
     */
/*加工订单*/
    @ViewInject(R.id.send_view1)
    LinearLayout send_view1;
    @ViewInject(R.id.service_lj_name)
    EditText     service_lj_name;
    /*机床耗材*/
    @ViewInject(R.id.send_view2)
    LinearLayout send_view2;
    @ViewInject(R.id.service_jc_name)
    EditText     service_jc_name;
    @ViewInject(R.id.service_jc_brand)
    EditText     service_jc_brand;
    @ViewInject(R.id.service_jc_type)
    EditText     service_jc_type;
    /*机床配件*/
    @ViewInject(R.id.send_view3)
    LinearLayout send_view3;
    @ViewInject(R.id.service_jcpj_name)
    EditText     service_jcpj_name;
    @ViewInject(R.id.service_jcpj_brand)
    EditText     service_jcpj_brand;
    @ViewInject(R.id.service_jcpj_type)
    EditText     service_jcpj_type;
    /*机床组立*/
    @ViewInject(R.id.send_view4)
    LinearLayout send_view4;
    @ViewInject(R.id.service_jczl_project)
    EditText     service_jczl_project;
    @ViewInject(R.id.service_jczl_brand)
    EditText     service_jczl_brand;
    @ViewInject(R.id.service_jczl_size_type)
    EditText     service_jczl_size_type;
    @ViewInject(R.id.service_jczl_type)
    EditText     service_jczl_type;
    @ViewInject(R.id.service_jczl_system_type)
    EditText     service_jczl_system_type;
    @ViewInject(R.id.service_jczl_personnum)
    EditText     service_jczl_personnum;
    /*二手设备买卖*/
    @ViewInject(R.id.send_view5)
    LinearLayout send_view5;
    @ViewInject(R.id.service_sbmm_jcxh)
    EditText     service_sbmm_jcxh;
    @ViewInject(R.id.service_sbmm_xtxh)
    EditText     service_sbmm_xtxh;
    @ViewInject(R.id.service_sbmm_jcpp)
    EditText     service_sbmm_jcpp;
    @ViewInject(R.id.service_sbmm_date)
    EditText     service_sbmm_date;
    @ViewInject(R.id.service_sbmm_num)
    EditText     service_sbmm_num;
    /*二手设备改造*/
    @ViewInject(R.id.send_view6)
    LinearLayout send_view6;
    @ViewInject(R.id.service_sbgz_gznr)
    EditText     service_sbgz_gznr;
    @ViewInject(R.id.service_sbgz_jcxh)
    EditText     service_sbgz_jcxh;
    @ViewInject(R.id.service_sbgz_xtxh)
    EditText     service_sbgz_xtxh;
    @ViewInject(R.id.service_sbgz_jcpp)
    EditText     service_sbgz_jcpp;

    /**
     * 表尾部信息
     */
    @ViewInject(R.id.contacts_name)
    EditText   contacts_name;
    @ViewInject(R.id.contacts_work)
    EditText   contacts_work;
    @ViewInject(R.id.contacts_tel)
    EditText   contacts_tel;
    @ViewInject(R.id.company_name)
    EditText   company_name;
    @ViewInject(R.id.company_address)
    EditText   company_address;
    @ViewInject(R.id.myGridView)
    MyGridView myGridView;
    /**
     * 发布
     */
    @ViewInject(R.id.btn_public)
    TextView   btn_public;

    private TakeImage takeHeadImage;
    private TakeImage takeImage;

    private int    pre_type_id;
    private String imgStr;

    private QImageAdapter adapter;

    //    0头像1底部图片
    private int    type;
    //    底部图片选择索引
    private int    curImg_index;
    //    type:1需求商 2个人服务商 3企业服务商
    private String account_type;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);

    }

    @Override
    public int getContentView() {
        return R.layout.activityq_send_service;
    }

    @Override
    public void initView() {

        ViewUtils.inject(this);
        pre_type_id = getIntent().getIntExtra("type_id", 0);
        account_type = PreferenceUtils.getPrefString("account_type", "");
        takeHeadImage = new TakeImage(this, handler, true);
        takeImage = new TakeImage(this, handler, false);
        adapter = new QImageAdapter(this);
        adapter.getDataList().add(new ImgObj(0, 0, null, null));
        myGridView.setAdapter(adapter);

        //
        String address = PreferenceUtils.getPrefString("address", "");
        company_address.setText(address);

    }

    @Override
    public void initData() {
        setTitle(pre_type_id);
    }

    @Override
    public void initEvent() {
        adapter.setBackCall(new BackCall() {
            @Override
            public void deal(int which, Object... obj) {
                super.deal(which, obj);
                type = 1;
                curImg_index = (Integer) obj[0];
                takeImage.showDialog();

            }
        });
        takeHeadImage.setOnPicClipListener(new TakeImage.PicClipListener() {
            @Override
            public void picClipCall(Bitmap bitmap) {
                QLBitmapUtil.saveBitmap(bitmap,
                        FileFinal.PATH_TEMP
                                + FileFinal.USER_IMG_HAED);
                imgStr = Base64Util.bitmaptoString(bitmap);
                title_img.setImageBitmap(bitmap);
            }
        });

        takeImage.setOnPictureListener(new TakeImage.PictureListener() {
            @Override
            public void pictureCall(String path) {

                ImgObj imgObj = adapter.getDataList().get(curImg_index);
                imgObj.setLocImgPath(path);
                imgObj.setHaveImg(2);

                if (adapter.getDataList().size() < 3)
                    adapter.getDataList().add(new ImgObj(0, 0, null, null));
                sendMsg(handler, 2, null);
            }
        });
    }

    @Override
    protected void updateUI(Message msg) {
        switch (msg.what) {
            case 1:
                ToastUtils.showToast(getString(R.string.qSendService_public_success));
                closeProgressDialog();
                setResult(1);
                finish();
                break;
            case 2:
                adapter.notifyDataSetChanged();
                break;


        }
    }

    @OnClick({R.id.back, R.id.btn_public, R.id.title_img})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.btn_public:
                if (TextUtils.equals(account_type,"2")||TextUtils.equals(account_type,"3")) {
                    sendBtn(pre_type_id);
                } else {
                    ToastUtils.showToast(getString(R.string.qSendService_service_can_puservice));
                }
                break;

            case R.id.title_img:
                type = 0;
                takeImage.showDialog();
                break;
        }
    }

    public void sendBtn(int pre_typeId) {
        boolean isPublic = false;
        PublicServiceReq publicServiceReq = new PublicServiceReq();
        switch (pre_typeId) {
            case 1:
                isPublic = headCheck();
                break;
            case 2:
                publicServiceReq.setJgdd_part_name(service_lj_name.getText().toString());
                break;
            case 3:
                publicServiceReq.setJchc_consumable_name(service_jc_name.getText().toString());
                publicServiceReq.setJchc_consumable_brand(service_jc_brand.getText().toString());
                publicServiceReq.setJchc_consumable_specifications(service_jc_type.getText().toString());
                break;
            case 4:

                publicServiceReq.setJcpj_accessories_name(service_jcpj_name.getText().toString());
                publicServiceReq.setJcpj_accessories_brand(service_jcpj_brand.getText().toString());
                publicServiceReq.setJcpj_accessories_specifications(service_jcpj_type.getText().toString());
                break;

            case 5:
                publicServiceReq.setJczl_group_project(service_jczl_project.getText().toString());
                publicServiceReq.setJczl_group_brand(service_jczl_brand.getText().toString());
                publicServiceReq.setJczl_group_specifications(service_jczl_size_type.getText().toString());
                publicServiceReq.setJczl_group_machine(service_jczl_system_type.getText().toString());
                publicServiceReq.setJczl_group_number(service_jczl_personnum.getText().toString());
                break;

            case 6:

                publicServiceReq.setEshsbmm_machine(service_sbmm_jcxh.getText().toString());
                publicServiceReq.setEshsbmm_system(service_sbmm_xtxh.getText().toString());
                publicServiceReq.setEshsbmm_machine_brands(service_sbmm_jcpp.getText().toString());
                publicServiceReq.setEshsbmm_machine_production_date(service_sbmm_date.getText().toString());
                publicServiceReq.setEshsbmm_machine_number(service_sbmm_num.getText().toString());

                break;

            case 7:
                publicServiceReq.setEssbgz_modified_content(service_sbgz_gznr.getText().toString());
                publicServiceReq.setEssbgz_machine(service_sbgz_jcxh.getText().toString());
                publicServiceReq.setEssbgz_system(service_sbgz_xtxh.getText().toString());
                publicServiceReq.setEssbgz_machine_brands(service_sbgz_jcpp.getText().toString());
                break;
        }

        if (isPublic) {
            List<ParamObj> paramObjs = new ArrayList<ParamObj>();
            paramObjs.add(new ParamObj("user_id", PreferenceUtils.getPrefString("user_id", "")));
            publicServiceReq.setType(pre_type_id);
            publicServiceReq.setTitle_img(new ReqImgObj(imgStr));
            publicServiceReq.setTitle(title.getText().toString());
            publicServiceReq.setDescribe(service_desc.getText().toString());
            publicServiceReq.setReference_price_low(service_price_start.getText().toString());
            publicServiceReq.setReference_price_high(service_price_end.getText().toString());
            publicServiceReq.setContact(contacts_name.getText().toString());
            publicServiceReq.setContact_position(contacts_work.getText().toString());
            publicServiceReq.setContact_phone(contacts_tel.getText().toString());
            publicServiceReq.setCompany_name(company_name.getText().toString());
            publicServiceReq.setCompany_address(company_address.getText().toString());

            ArrayList<ReqImgObj> reqImgObjs = new ArrayList<ReqImgObj>();
            try {
                int length = adapter.getDataList().size();
                for (int i = 0; i < length; i++) {
                    if (i == (length - 1) && adapter.getDataList().get(i).getHaveImg() == 0)
                        break;
                    reqImgObjs.add(new ReqImgObj(Base64Util.encodeBase64File(adapter.getDataList().get(i).getLocImgPath())));
                }
            } catch (Exception e) {
                ToastUtils.showToast(getString(R.string.QPostForumActivity_img_switch_failure));
            }

            publicServiceReq.setImg_list(reqImgObjs);

            new PublicServiceHttp(this, handler, GetHaveParam(paramObjs), publicServiceReq, 1);
        }
    }

    private boolean headCheck() {

        if (TextUtils.isEmpty(imgStr)) {
            ToastUtils.showToast( getString(R.string.qSendService_add_img));
            return false;
        }
        if (TextUtils.isEmpty(service_title.getText().toString())) {
            ToastUtils.showToast( getString(R.string.qSendService_please_input_title));
            return false;
        }
        if (TextUtils.isEmpty(service_desc.getText().toString())) {
            ToastUtils.showToast( getString(R.string.qSendService_please_input_desc));
            return false;
        }
        if (TextUtils.isEmpty(service_price_start.getText().toString())) {
            ToastUtils.showToast( getString(R.string.qSendService_please_input_price));
            return false;
        }
        if (TextUtils.isEmpty(service_price_end.getText().toString())) {
            ToastUtils.showToast( getString(R.string.qSendService_please_input_price));
            return false;
        }

        if (TextUtils.isEmpty(contacts_name.getText().toString())) {
            ToastUtils.showToast( getString(R.string.qSendService_please_input_contact));
            return false;
        }
        if (TextUtils.isEmpty(contacts_work.getText().toString())) {
            ToastUtils.showToast(getString(R.string.qSendService_please_input_contact_work));
            return false;
        }
        if (TextUtils.isEmpty(contacts_tel.getText().toString())) {
            ToastUtils.showToast( getString(R.string.qSendService_please_input_contact_tel));
            return false;
        }
        if (TextUtils.isEmpty(company_name.getText().toString())) {
            ToastUtils.showToast( getString(R.string.qSendService_please_input_company_name));
            return false;
        }
        if (TextUtils.isEmpty(company_address.getText().toString())) {
            ToastUtils.showToast( getString(R.string.qSendService_please_input_company_address));
            return false;
        }

        return true;
    }

    public void setTitle(int pre_typeId) {
        String str_type = null;
        setModelView();
        switch (pre_typeId) {
            case 1:
                str_type = getString(R.string.qfacilitym_str_type_1);
                break;
            case 2:
                str_type = getString(R.string.qfacilitym_str_type_2);
                send_view1.setVisibility(View.VISIBLE);
                break;
            case 3:
                send_view2.setVisibility(View.VISIBLE);
                str_type = getString(R.string.qfacilitym_str_type_3);

                break;
            case 4:
                send_view3.setVisibility(View.VISIBLE);
                str_type = getString(R.string.qfacilitym_str_type_4);
                break;
            case 5:
                send_view4.setVisibility(View.VISIBLE);
                str_type = getString(R.string.qfacilitym_str_type_5);
                break;
            case 6:
                send_view5.setVisibility(View.VISIBLE);
                str_type = getString(R.string.qfacilitym_str_type_6);
                break;
            case 7:
                send_view6.setVisibility(View.VISIBLE);
                str_type = getString(R.string.qfacilitym_str_type_7);
                break;
        }
        title.setText(getString(R.string.qSendService_title) + "  " + str_type);
        service_type_tv.setText(str_type);
    }

    private void setModelView() {
        send_view1.setVisibility(View.GONE);
        send_view2.setVisibility(View.GONE);
        send_view3.setVisibility(View.GONE);
        send_view4.setVisibility(View.GONE);
        send_view5.setVisibility(View.GONE);
        send_view6.setVisibility(View.GONE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (type) {
            case 0:
                takeHeadImage.onActivityResult(requestCode, resultCode, data);
                break;
            case 1:
                takeImage.onActivityResult(requestCode, resultCode, data);
                break;
        }
    }

}
